package com.xc.freeapp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cloopen.rest.sdk.utils.DateUtil;
import com.xc.freeapp.dao.CollectdoctorDao;
import com.xc.freeapp.dao.DoctorDao;
import com.xc.freeapp.dao.dataSource.DBContextHolder;
import com.xc.freeapp.entity.Collectdoctor;
import com.xc.freeapp.entity.Doctor;
import com.xc.freeapp.service.CollectDoctorService;
import com.xc.freeapp.util.DateUtils;
import com.xc.freeapp.util.JsonUtil;

@Service
public class CollectDoctorServiceImpl implements CollectDoctorService {
	@Autowired
	CollectdoctorDao collectdoctorDao;
	
	
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public Map<String, Object> collectDoctor(Map<String, String> map) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// TODO Auto-generated method stub
		String userId = map.get("userId");
		String doctorid = map.get("doctorid");
		Collectdoctor collectdoctor = null;
		collectdoctor = collectdoctorDao.selectByUseridAndDoctorid(
				Integer.valueOf(userId), Integer.valueOf(doctorid));
		collectdoctorDao.addconllect(doctorid);
		int i;
		if (collectdoctor == null) {
			
			collectdoctor = new Collectdoctor();
			collectdoctor.setUserid(Integer.parseInt(userId));
			collectdoctor.setDoctorid(Integer.parseInt(doctorid));
			collectdoctor.setCreatetime(DateUtils.getDate());
			collectdoctor.setUpdatetime(DateUtils.getDate());
			collectdoctor.setDelflag("t");
			i = collectdoctorDao.insert(collectdoctor);
		} else {
			collectdoctor.setCreatetime(DateUtils.getDate());
			collectdoctor.setUpdatetime(DateUtils.getDate());
			collectdoctor.setDelflag("t");
			i = collectdoctorDao.updateByPrimaryKey(collectdoctor);
		}

		if (i == 1) {
			resultMap.put("code", "1");
			resultMap.put("msg", "收藏成功");
		} else {
			resultMap.put("code", "0");
			resultMap.put("msg", "收藏失败");
		}
		return resultMap;
	}
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public Map<String, Object> delCollectDoctor(Map<String, String> map) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// TODO Auto-generated method stub
		String doctorid = map.get("doctorid");
		String userid = map.get("userid");

		int i = collectdoctorDao.deleteByDoctorId(Integer.valueOf(doctorid),
				Integer.valueOf(userid));
		collectdoctorDao.reduceconllect(doctorid);
		if (i == 1) {
			resultMap.put("code", "1");
			resultMap.put("msg", "删除收藏成功");
		} else {
			resultMap.put("code", "0");
			resultMap.put("msg", "删除收藏失败");
		}
		return resultMap;
	}
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public List<Collectdoctor> getCollectDoctor(Map<String, String> map) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		List<Collectdoctor> collectdoctors = null;
		try {
			Map<String, Object> resultmap = new HashMap<String, Object>();
			String userId = map.get("userId");
			collectdoctors = collectdoctorDao.selectByUserId(Integer
					.parseInt(userId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return collectdoctors;
	}

}
